package com.b1502.store2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import android.widget.ImageView;


import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.model.Product;



import com.b1502.store2.model.bean.HomeProductsBean;

import com.b1502.store2.util.ImageLoaderUtils;


import com.b1502.store2.util.UrlUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * 1. 主页产品适配器
 * 2. @author：liqingyi
 * 3. @date：2017/2/27 14:22
 */

public class HomeProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{

    HomeProductsBean[]
            products;
    Context context;
    DisplayImageOptions options;

    public HomeProductAdapter(Context context, HomeProductsBean[] products) {
        this.context = context;
        this.products = products;
        options= ImageLoaderUtils.initOptions();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_other, parent, false);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return new ProductHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ProductHolder productHolder= (ProductHolder) holder;
        productHolder.itemView.setTag(position);
        ImageLoader.getInstance().displayImage(UrlUtil.getImageUrl(products[position].getImagePath()),productHolder.imageView,options);
        productHolder.textView1.setText(products[position].getName());
        productHolder.textView2.setText("￥"+products[position].getPrice()+"");

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return products.length;
    }

    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }

    }

    static class ProductHolder extends RecyclerView.ViewHolder {

        ImageView imageView;

        TextView textView1,textView2;


        public ProductHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.home_item_image);
            textView1= (TextView) itemView.findViewById(R.id.home_item_name);
            textView2= (TextView) itemView.findViewById(R.id.home_item_price);
        }

    }

    public static interface OnRecyclerViewItemClickListener {

        void onItemClick(View view, int position);
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
